//
//  WYCForgetController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/3.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCForgetController.h"
#import "UIButton+CountDown.h"
#import "RSAUtil.h"
#import "WYCLoginController.h"

@interface WYCForgetController ()
/** 账号输入框*/
@property(nonatomic,strong)UITextField * UserTextField;

/** 密码*/
@property(nonatomic,strong)UITextField * passtexfield;


/** 再次输入密码*/
@property(nonatomic,strong)UITextField * Zpasstexfield;

/** 验证码*/
@property(nonatomic,strong)UITextField * codeTexField;


/** 邀请码*/
@property(nonatomic,strong)UITextField * Rgscodefield;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * ZpasstexfieldButton;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * passtexfieldButton;
@end

@implementation WYCForgetController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建界面
    [self setupCreat];
}

#pragma make ------>>界面创建
- (void)setupCreat{
    
    self.view.backgroundColor = wycwhiteColor;
    
    
    
    UIView * registerView = [[UIView alloc] init];
    registerView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    registerView.backgroundColor = WYCMain;
    [self.view addSubview:registerView];
    
    
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 200 * px, 30, 400 * px, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    //    NavRigsterLabel.font = [UIFont systemFontOfSize:BigFont];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"忘记密码";
    [registerView addSubview:NavRigsterLabel];
    
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(30 * px, NavRigsterLabel.y, 66 * px , 66 * px);
    [registerView addSubview:backbutton];
    
    
   
    
    ////////////////
    //账号输入框/////
    ///////////////
    
    UILabel * chinalabel = [[UILabel alloc] init];
    chinalabel.frame = CGRectMake(160 * px, CGRectGetMaxY(registerView.frame) + 80 * px, 150 * px, 120 * px);
    chinalabel.text = @"+86";
    chinalabel.font = [UIFont systemFontOfSize:MiddleFont];
    chinalabel.textColor = [UIColor grayColor];
    [self.view addSubview:chinalabel];
    
    UITextField * usertexfield = [[UITextField alloc] init];
    usertexfield.frame = CGRectMake(CGRectGetMaxX(chinalabel.frame),CGRectGetMaxY(registerView.frame) + 80 * px, ScreenWidth - 160 * px - chinalabel.width * 2, 120 * px);
    //    usertexfield.backgroundColor = [UIColor redColor];
    usertexfield.placeholder = @"请输入手机号";
    [self.view addSubview:usertexfield];
    _UserTextField = usertexfield;
    
    
    
    UIButton * usertexfieldButton = [[UIButton alloc] init];
    usertexfieldButton.frame = CGRectMake(CGRectGetMaxX(usertexfield.frame) - 80 * px, usertexfield.y, 80 * px, 80 * px);
    [usertexfieldButton addTarget:self action:@selector(usertexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [usertexfieldButton setImage:[UIImage imageNamed:@"celar"] forState:UIControlStateNormal];
    [self.view addSubview:usertexfieldButton];
    
    //手机提示线
    UIView * lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(80 * px, CGRectGetMaxY(usertexfield.frame) + 5 * px, ScreenWidth - 160 * px, 1);
    lineView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:lineView];
    
    
    ////////////////
    /////密码输入框/////
    ///////////////
    
    
    
    
    UITextField * codefield = [[UITextField alloc] init];
    codefield.frame = CGRectMake(chinalabel.x, CGRectGetMaxY(lineView.frame) + 80 * px, usertexfield.width / 2, usertexfield.height);
    codefield.keyboardType = UIKeyboardTypeNumberPad;
    codefield.placeholder = @"验证码";
    [self.view addSubview:codefield];
    self.codeTexField = codefield;
    
    //手机提示线
    UIView * passlineView = [[UIView alloc] init];
    passlineView.frame = CGRectMake(lineView.x, CGRectGetMaxY(codefield.frame) + 5 * px, lineView.width, 1);
    passlineView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:passlineView];
    
    //获取验证码
    UIButton * codebutton = [[UIButton alloc] init];
    codebutton.frame = CGRectMake(ScreenWidth - 460 * px, codefield.y, 300 * px, codefield.height);
    [codebutton setTitleColor:WYCMain forState:UIControlStateNormal];
    codebutton.titleLabel.font = [UIFont systemFontOfSize:MiddleFont];
    [codebutton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [codebutton addTarget:self action:@selector(countDownBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:codebutton];
    
    
    
    ////////////////
    //密码输入框/////
    ///////////////
    
    
    UITextField * passtexfield = [[UITextField alloc] init];
    passtexfield.frame = CGRectMake(chinalabel.x, CGRectGetMaxY(passlineView.frame) + 80 * px, usertexfield.width, usertexfield.height);
    passtexfield.placeholder = @"请输入密码";
    passtexfield.secureTextEntry = YES;
    [self.view addSubview:passtexfield];
    _passtexfield = passtexfield;
    
    
    UIButton * passtexfieldButton = [[UIButton alloc] init];
    passtexfieldButton.frame = CGRectMake(usertexfieldButton.x, passtexfield.y, 80 * px, 80 * px);
    [passtexfieldButton addTarget:self action:@selector(passtexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [passtexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
    [passtexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
    [self.view addSubview:passtexfieldButton];
    
    //手机提示线
    UIView * passView = [[UIView alloc] init];
    passView.frame = CGRectMake(lineView.x, CGRectGetMaxY(passtexfield.frame) + 5 * px, lineView.width, 1);
    passView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:passView];
    
    
    
    
    /************再次输入密码************/
    
    
    UITextField * Zpasstexfield = [[UITextField alloc] init];
    Zpasstexfield.frame = CGRectMake(chinalabel.x, CGRectGetMaxY(passView.frame)+80 * px, usertexfield.width, usertexfield.height);
    Zpasstexfield.placeholder = @"再次输入密码";
    Zpasstexfield.secureTextEntry = YES;
    [self.view addSubview:Zpasstexfield];
    _Zpasstexfield = Zpasstexfield;
    
    
    UIButton * ZpasstexfieldButton = [[UIButton alloc] init];
    ZpasstexfieldButton.frame = CGRectMake(usertexfieldButton.x, Zpasstexfield.y, 80 * px, 80 * px);
    [ZpasstexfieldButton addTarget:self action:@selector(ZpasstexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [ZpasstexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
    [ZpasstexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
    [self.view addSubview:ZpasstexfieldButton];
    
    //手机提示线
    UIView * ZpassView = [[UIView alloc] init];
    ZpassView.frame = CGRectMake(chinalabel.x, CGRectGetMaxY(Zpasstexfield.frame) + 5 * px, lineView.width, 1);
    ZpassView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:ZpassView];
    
    
    
    //注册按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(lineView.x, CGRectGetMaxY(ZpassView.frame) + 300 * px, ScreenWidth - 160 * px, 150 * px);
    loginButton.backgroundColor = WYCMain;
    loginButton.layer.cornerRadius = 3;
    [loginButton setTitle:@"找回" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:72 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(RegisterClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    
}
//键盘回收
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_UserTextField resignFirstResponder];
    [_codeTexField resignFirstResponder];
    [_passtexfield resignFirstResponder];
    [_Zpasstexfield resignFirstResponder];
}

#pragma make ------>>获取验证码
- (void)countDownBtnAction:(UIButton *)button
{
    WYCLog(@"----------%@",self.UserTextField.text);
    WYCLogFunc;
    if (self.UserTextField.text.length == 0) {
        
        NSString * showStr = @"手机号不能为空";
        [self showToast:showStr];
        return;
    }else{
        [button startWithTime:59 title:@"点击重新获取" countDownTitle:@"s后重新获取" mainColor:[UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0] countColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
        NSString * httpsStr = [NSString stringWithFormat:@"%@mobile=%@&type=%@",Rcode,self.UserTextField.text,@"fpwd"];
        
        [HttpTool POST:httpsStr params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            WYCLog(@"验证码%@",responseObject);
            
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                [self showToast:msgStr];
                //                [self showToast:msgStr];
                NSDictionary * dict = responseObject[@"result"];
                
                WYCAccount * Account = [[WYCAccount alloc] initWithDic:dict];
                
                [WYCAccountTool saveAccount:Account];
                
                [WYCIsAppLoginTool saveIsAppLogin:YES];
                
            }else{
                
                [self showToast:msgStr];
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            [self showToast:Failed];
        }];
        
    }
}

#pragma make ------>>注册按钮
- (void)RegisterClick {
    WYCLogFunc;
    // 手机号判断是否为空
    if(self.UserTextField.text.length == 0)
    {
        [self showToast:@"手机号不能为空"];
        return;
    }
    //验证码判断是否为空
    if(self.codeTexField.text.length == 0)
    {
        [self showToast:@"请输入验证码"];
        return;
    }
    //手机号判断是否为空
    if(self.passtexfield.text.length == 0)
    {
        [self showToast:@"密码不能为空"];
        return;
    }
    //密码判断是否为空
    
    if(![self.passtexfield.text isEqualToString:self.Zpasstexfield.text])
    {
        [self showToast:@"两次输入密码不一致"];
        return;
    }
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    NSString * passStr = [NSString stringWithFormat:@"%@",self.passtexfield.text];
    NSString *pas = [RSAUtil encryptString:passStr publicKey:RSA_P_KEY];
    //
    
    params[@"ticket"] = [WYCAccountTool unarchiveticket];
    params[@"authcode"] = self.codeTexField.text;
    params[@"newPwd"] = pas;
    
    WYCLog(@"--------%@",params);
    [HttpTool POST:safemodifyPasswordByForget params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        WYCLog(@"找回密码%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            [self showToast:@"找回成功"];
            [self backClick];
        }else{
            [self showToast:msgStr];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [self showToast:Failed];
    }];
    
}
- (void)passtexfieldButtonClick{
    self.passtexfieldButton.selected = !self.passtexfieldButton.selected;
    if (self.passtexfieldButton.selected == YES) {
        self.passtexfield.secureTextEntry = NO;
    }else{
        
        self.passtexfield.secureTextEntry = YES;
    }
    
}

- (void)ZpasstexfieldButtonClick{
    self.ZpasstexfieldButton.selected = !self.ZpasstexfieldButton.selected;
    if (self.Zpasstexfield.selected == YES) {
        self.Zpasstexfield.secureTextEntry = NO;
    }else{
        
        self.Zpasstexfield.secureTextEntry = YES;
    }
    
}
- (void)usertexfieldButtonClick{
    
    self.UserTextField.text = @"";
}



#pragma make ------>>返回按钮
- (void)backClick{
    [self dismissViewControllerAnimated:NO completion:nil];
}
@end


